package com.wzdigit.wms.wms.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.basic.Sku;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.wip.WipGoodsMapper;
import com.wzdigit.wms.wms.dto.inventory.GoodsDto;
import com.wzdigit.wms.wms.rpc.inventory.GoodsApiImplService;
import com.wzdigit.wms.wms.service.core.AsnManagerService;
import com.wzdigit.wms.wms.service.task.GoodsDtoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created by zyf on 2021/12/29 19:43
 */
@Service
@Slf4j
public class IfGoodsSyncService {
    @Autowired
    private WipGoodsMapper wipGoodsMapper;

    @Autowired
    private GoodsDtoService goodsDtoService;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private AsnManagerService asnManagerService;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public synchronized void syncData(GoodsDto goodsDto,Integer dataId) throws Exception {
        try {
            if (goodsDto.getSkuCode() == null) {
                String errMsg = goodsDto.getGoodsSn() + "下线同步失败,未传入SKU_CODE";
                log.error(errMsg);
                throw new RuntimeException(errMsg);
            }


            Sku sku1 = skuMapper.selectOne(new QueryWrapper<Sku>().eq("SKU_CODE",goodsDto.getSkuCode()));

            if (sku1 == null || sku1.getSkuId() == null) {
                String msg = String.format("根据产品code:【%s】在货品主数据表WMS_SKU中未找到数据", goodsDto.getSkuCode());
                log.warn(msg);

                String errMsg = goodsDto.getGoodsSn() + "下线同步失败";
                log.error(errMsg);
                throw new RuntimeException(msg);
            }
            if (goodsDto.getMfgLine()==null||"".equals(goodsDto.getMfgLine())){
                String msg = String.format("线体【%s】为空", goodsDto.getMfgLine());
                log.warn(msg);

                String errMsg = goodsDto.getGoodsSn() + "下线同步失败";
                log.error(errMsg);
                throw new RuntimeException(msg);
            }
            if (goodsDto.getPrdWo()==null||"".equals(goodsDto.getPrdWo())){
                String msg = String.format("工单【%s】为空", goodsDto.getPrdWo());
                log.warn(msg);

                String errMsg = goodsDto.getGoodsSn() + "下线同步失败";
                log.error(errMsg);
                throw new RuntimeException(msg);
            }
            if (goodsDto.getFactory()==null||"".equals(goodsDto.getFactory())){
                String msg = String.format("组织【%s】为空", goodsDto.getFactory());
                log.warn(msg);

                String errMsg = goodsDto.getGoodsSn() + "下线同步失败";
                log.error(errMsg);
                throw new RuntimeException(msg);
            }
            if (goodsDto.getPoNo()==null||"".equals(goodsDto.getPoNo())){
                String msg = String.format("订单号【%s】为空", goodsDto.getPoNo());
                log.warn(msg);

                String errMsg = goodsDto.getGoodsSn() + "下线同步失败";
                log.error(errMsg);
                throw new RuntimeException(msg);
            }

            //查询当前工单的SN在GOODS表有吗，存在就更新WMS_FLAG=1且直接返回
            List<String> snList = asnManagerService.searchIsReceive(goodsDto.getMfgLine(),
                    sku1.getSkuId(), goodsDto.getPrdWo(), Arrays.asList(goodsDto.getGoodsSn()));
            if (CollectionUtil.isNotEmpty(snList)){
                log.info(goodsDto.getGoodsSn()+"当前工单已经下线，更新为已同步");
                wipGoodsMapper.updateSucessWmsFlag(dataId);
                return;
            }


            goodsDtoService.addOneGoods(null,goodsDto);
            wipGoodsMapper.updateSucessWmsFlag(dataId);
        }catch (Exception e){
            Shift.fatal(e.getMessage());
        }
    }


}
